package com.ar.logreader.reader;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Properties;

public class LogReader {
	
	private static Boolean continueMonitoring = Boolean.FALSE;
	
	private final String logPath = "C:\\Program Files (x86)\\NCSoft\\Aion\\Chat.log";
	
	private Properties timers;
	
	private DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
	
	private Map<String, GodstoneTimer> godstonesMap;
	
	private Timestamp SILENCE, PARA, BLEED, BLIND, SNARE, SLOW, STUN, ROOT, POISON;
	
	private Boolean SILENCE_FLAG, PARA_FLAG, BLEED_FLAG, BLIND_FLAG, SNARE_FLAG, 
					SLOW_FLAG, STUN_FLAG, ROOT_FLAG, POISON_FLAG, DMG_FLAG;

	public Boolean getContinueMonitoring() {
		return continueMonitoring;
	}

	public static void setContinueMonitoring(Boolean continueProcess) {
		continueMonitoring = continueProcess;
	}
	
	public void read() {
		try {
			BufferedReader  input = new BufferedReader(new FileReader(this.logPath));
			String line, lastLine;
			
			Date asd = new Date();
			asd.getSeconds();
			while(continueMonitoring) {
				line = input.readLine();
				if(line != null && this.containGodStones(line)){
					
				} else {
					line=null;
					Thread.sleep(50);
				}
				lastLine=line;
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.out.println("Can't find log file: " + this.logPath);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}

	private boolean containGodStones(String line) {
		Boolean containGodStones = Boolean.FALSE;
		
		if(line.contains(this.timers.getProperty("SILENCE_PROC_TEXT"))){
			containGodStones = Boolean.TRUE;
			this.SILENCE_FLAG = Boolean.TRUE;
		} else if(line.contains(this.timers.getProperty("PARA_PROC_TEXT"))){
			containGodStones = Boolean.TRUE;
			this.PARA_FLAG = Boolean.TRUE;
		} else if(line.contains(this.timers.getProperty("BLEED_PROC_TEXT"))){
			containGodStones = Boolean.TRUE;
			this.BLEED_FLAG = Boolean.TRUE;
		} else if(line.contains(this.timers.getProperty("BLIND_PROC_TEXT"))){
			containGodStones = Boolean.TRUE;
			this.BLIND_FLAG = Boolean.TRUE;			
		} else if(line.contains(this.timers.getProperty("SNARE_PROC_TEXT"))){
			containGodStones = Boolean.TRUE;
			this.SNARE_FLAG = Boolean.TRUE;			
		} else if(line.contains(this.timers.getProperty("SLOW_PROC_TEXT"))){
			containGodStones = Boolean.TRUE;
			this.SLOW_FLAG = Boolean.TRUE;
		} else if(line.contains(this.timers.getProperty("STUN_PROC_TEXT"))){
			containGodStones = Boolean.TRUE;
			this.STUN_FLAG = Boolean.TRUE;
		} else if(line.contains(this.timers.getProperty("ROOT_PROC_TEXT"))){
			containGodStones = Boolean.TRUE;
			this.ROOT_FLAG = Boolean.TRUE;
		} else if(line.contains(this.timers.getProperty("POISON_PROC_TEXT"))){
			containGodStones = Boolean.TRUE;
			this.POISON_FLAG = Boolean.TRUE;
		} else if(line.contains(this.timers.getProperty("DMG_PROC_TEXT"))){
			containGodStones = Boolean.TRUE;
			this.DMG_FLAG = Boolean.TRUE;
		}
		
		return containGodStones;
	}

}